﻿/**
 * particles
 *
 * @author		Duarte Peixinho
 * @version		1.0
 */

package com.duartepeixinho.particles.core
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import com.duartepeixinho.particles.core.Particle;
	
	public class CreateParticlesFromImage extends Sprite
	{		
		private var _bitmapdata:BitmapData;		
		private var _xPixel:uint;
		private var _yPixel:uint;
		
		protected var _pixels:Array = new Array();
		
		/**
		 * createPixels
		 * 
		 * @param		image		BitmapData		Original Bitmap
		 * @param		xPixel		uint			width of pixels
		 * @param		yPixel		uint			height of pixels
		 */ 
		public function CreateParticlesFromImage(object:DisplayObject, xPixel:uint, yPixel:uint, backgroundColor:Number = 0xffffff) 
		{
			_xPixel = xPixel;
			_yPixel = yPixel;

			//drawing to bitmapdata
			var oRect:Rectangle = object.getBounds(object);
			var matrix:Matrix = new Matrix();
			matrix.translate( -oRect.x, -oRect.y);
			
			_bitmapdata = new BitmapData(object.width, object.height,true,backgroundColor);
			_bitmapdata.draw(object, matrix);						
						
			
			//creating pixels			
			//setting counter
			var l:uint = 0;
			//width			
			for (var i:uint = 0; i < _bitmapdata.width; i += _xPixel) {
				//height
				for (var j:uint = 0; j < _bitmapdata.height; j += _yPixel) {
					
					//creating bitmapdata
					var pixelB:BitmapData = new BitmapData(_xPixel, _yPixel,true,backgroundColor);
					
					//setting pixels
					pixelB.copyPixels(_bitmapdata, new Rectangle(i, j, i + _xPixel, j + _yPixel), new Point(0, 0));
					
					//searching colors
						
						var pixel:Particle = new Particle(pixelB, i + oRect.x, j + oRect.y,0,i + oRect.x,j + oRect.y);
						_pixels.push(pixel);
						l++;
					
				}
			}	
		}
		
		/**
		 * getPixels
		 * @return			Array		Returns the array with the pixels
		 */
		public function getPixels():Array {
			return _pixels;
		}
		
	}
	
}